<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Account Model
 *
 * @package     VisualQ Local System
 * @author      CSU OJT Students '10 - '11 (Michael Roland B. Martin)
 * @copyright   Copyright (c) 2010 - 2011, Bitstop Network Services, Inc.
 * @version     1.0 January 27, 2011
 */

class Account_model extends Model {

    function Account_model()
    {
        parent::Model();
        $this->load->database();

        $this->CI =& get_instance();
    }

    /**
     * Fetch the agents data
     *
     * Used by:
     *      manager/statistics/
     *
     * @return  object
     *      query           :   query result
     */
    function get_agents_details()
    {
        $this->db->where('agent_id IS NOT NULL', NULL);
        return $this->db->get('accounts');
    }

    /**
     * Get an account data
     *
     * Used by:
     *      manager/account_info/
     *      account_model/get_account_type/
     *
     * @param	array
     *      id              :   account id
     * @return  object
     *      query           :   query result
     */
    function get_account($id)
    {
        return $this->db->get_where('accounts', array('id' => $id));
    }

    /**
     * Get account type
     *
     * @param	array
     *      id              :   account id
     * @return  string
     *      account_type    :   account type
     */
    function get_account_type($id)
    {
        $result = $this->get_account($id);
        $row = $result->row_array();

        return $row['account_type'];
    }

    /**
     * Get a country data
     *
     * Used by:
     *      manager/account_info/
     *
     * @param	array
     *      id              :   country id
     * @return  object
     *      query           :   query result
     */
    function get_country($id)
    {
        return $this->db->get_where('countries', array('id' => $id));
    }

    /**
     * Verify login details
     *
     * Used by:
     *      login/authenticate/
     *
     * @param	array
     *      un              :   username
     *      pw              :   password
     * @return  object
     *      query           :   query result
     */
    function verify_details($params)
    {
        $query = $this->db->get_where('accounts', array('username' => $params['un'], 'password' => md5($params['pw'])));

        return $query;
    }

    /**
     * Check in (DTR) the user
     *
     * Used by:
     *      login/authenticate/
     *
     * @param	array
     *      id              :   account id
     */
    function check_in($id) {
        $this->load->helper('date');

        $time = now();

        $dtr = array(
            'account_id'    => $id,
            'action'        => 0,
            'time'          => $time
        );

        $this->db->insert('dtr', $dtr);

        $this->db->where('id', $this->session->userdata('id'));
        $this->db->update('accounts', array('last_login' => $time));
    }

}

/* End of file account_model.php */
/* Location: ./system/application/model/account_model.php */